package com.yiren.manager.ctrl.department;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrFloor;
import com.yiren.entity.YrSubjectRoom;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.beds.YrFloorForm;
import com.yiren.manager.form.department.YrSubjectRoomForm;
import com.yiren.manager.service.iface.beds.YrFloorService;
import com.yiren.manager.service.iface.department.YrSubjectRoomService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("subjectRoom")
@Controller
public class YrSubjectRoomController extends GenericController<YrSubjectRoomForm, YrSubjectRoom>{

	@Autowired
	private YrSubjectRoomService selfService;
	
	@Autowired
	private YrFloorService floorService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrSubjectRoomForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrSubjectRoomForm> result = new HTTPResult<YrSubjectRoomForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrSubjectRoomForm form = tool.mapper("subjectRoom", YrSubjectRoomForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getSubjectRoomId()||form.getSubjectRoomId().isEmpty()){
			form.setSubjectRoomId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrSubjectRoom entity = ObjectUtils.objectToObject(form, YrSubjectRoom.class);
		selfService.save(entity);
		
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrSubjectRoomForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrSubjectRoomForm> result = new HTTPResult<YrSubjectRoomForm>();
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("departmentName", "ASC");
		map.put("createTime", "ASC");
		QueryResult<YrSubjectRoom> qr = selfService.find(YrSubjectRoom.class, null, new Object[]{},map);
		List<YrSubjectRoom> entityList = qr.getResultList();
		List<YrSubjectRoomForm> formList = new ArrayList<YrSubjectRoomForm>();
		for(YrSubjectRoom entity : entityList){
			YrSubjectRoomForm form = ObjectUtils.objectToObject(entity, YrSubjectRoomForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrSubjectRoomForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrSubjectRoomForm> result = new HTTPResult<YrSubjectRoomForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfService.delete(id);
		}
		return result;
	}
	
	@RequestMapping("findFloors")
	@ResponseBody
	public HTTPResult<YrFloorForm> findFloors(String floorsId) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		
		String[] ids = floorsId.split(",");
		
		List<YrFloorForm> formList = new ArrayList<YrFloorForm>();
		for(int i=0;i<ids.length;i++){
			YrFloor floor = floorService.findOne(ids[i]);
			if(null!=floor){
				formList.add(ObjectUtils.objectToObject(floor, YrFloorForm.class));
			}
		}
		
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrSubjectRoomForm toForm(YrSubjectRoom e) throws Exception {
		YrSubjectRoomForm form = ObjectUtils.objectToObject(e, YrSubjectRoomForm.class);
		return form;
	}

	@Override
	public YrSubjectRoom toEntity(YrSubjectRoomForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

		
}
